package com.enqualcomm.kids.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;


/**
 * Created by yinzhiqun on 2016/3/2.
 */
public class ReaiBookView extends RelativeLayout {
    public ReaiBookView(Context context) {
        this(context, null);
    }

    public ReaiBookView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ReaiBookView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setClickable(true);
    }

    private static final int STATE_TOUCH_DOWN = 1;
    private static final int STATE_MOVE_VERTICAL = 2;
    private static final int STATE_MOVE_HORIZONTAL_LEFT = 3;
    private static final int STATE_MOVE_HORIZONTAL_RIGHT = 4;
    float downX;
    float downY;
    int state;
    boolean intercept;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (listener!=null){
            switch (ev.getAction()){
                case MotionEvent.ACTION_DOWN:
                    downX = ev.getX();
                    downY = ev.getY();
                    state = STATE_TOUCH_DOWN;
                    intercept = false;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (state==STATE_TOUCH_DOWN){ //还没有判定结果
                        float moveX = ev.getX();
                        float moveY = ev.getY();
                        //根据x，y轴偏移量，判断是左右滑动还是上下滑动

                        if (Math.abs(moveY - downY) >400){//上下滑动超过200像素
                            state = STATE_MOVE_VERTICAL;
                        }else{
                            float offsetX = moveX - downX;
                            if (offsetX>200){
                                state = STATE_MOVE_HORIZONTAL_RIGHT;
                                intercept = true;
                                listener.onFlipRight();
                            }else if (offsetX<-200){
                                state = STATE_MOVE_HORIZONTAL_LEFT;
                                intercept = true;
                                listener.onFlipLeft();
                            }
                        }
                    }
                    break;
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (intercept){//阻止子控件响应
            return true;
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (intercept){//已经处理了,不需要再响应点击
            return true;
        }
        return super.onTouchEvent(event);
    }


    FlipListener listener;
    public void setOnFlipListener(FlipListener listener){
        this.listener = listener;
    }

    public interface FlipListener{
        /**
         * 往左翻页<--
         */
        void onFlipLeft();

        /**
         * 往右翻页-->
         */
        void onFlipRight();
    }

}
